//
//  RACTwoViewController.m
//  LockTest
//
//  Created by ZhangBo on 2018/12/3.
//  Copyright © 2018 ZhangBo. All rights reserved.
//

#import "RACTwoViewController.h"
#import "TestObject.h"
#import <ReactiveObjC/ReactiveObjC.h>


@interface RACTwoViewController ()
@property(nonatomic, strong) RACSubject *hotSignal;
@property(nonatomic, weak) IBOutlet UIButton *kButton;
@property(nonatomic, strong) TestObject *testObject;
@property(nonatomic, copy) NSString *name;
@end

@implementation RACTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self buttonSignal];
}

//  冷信号（订阅就能被触发信号）
- (void)signalUnit {

    // 创建一个信号
    RACSignal *signal1 = [RACSignal return:@"hello rac"];
    // 订阅一个信号
    [signal1 subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


    RACSignal *signal2 = [RACSignal error:[NSError errorWithDomain:@"123" code:404 userInfo:nil]];
    [signal2 subscribeError:^(NSError *error) {
        NSLog(@"%@", error);
    }];

    RACSignal *signal3 = [RACSignal empty];
    [signal3 subscribeNext:^(id x) {
        NSLog(@"empty:%@", x);
    }];

}

- (void)dynamicSignal {

    //创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        //发送信号
        [subscriber sendNext:@"123456"];

        [subscriber sendNext:@"654321"];

        return nil;
    }];
    //订阅信号
    [signal subscribeNext:^(id x) {
        NSLog(@"....%@", x);
    }];

}

- (void)hotSignal {
    _hotSignal = [RACSubject subject];
    [_hotSignal subscribeNext:^(id x) {
        NSLog(@"通知来了,%@", x);
    }];

    [_hotSignal subscribeNext:^(id x) {
        NSLog(@"通知又来了，%@", x);
    }];


}

- (void)buttonSignal {
    RACSignal *signal = [_kButton rac_signalForControlEvents:UIControlEventTouchUpInside];
    [signal subscribeNext:^(id _Nullable x) {
        NSLog(@"button 被点击啦");
    }];


    RACSignal *corlorSignal = [RACSignal return:[UIColor yellowColor]];
    RACSignal *eventSignal = [RACSignal return:@(UIControlStateHighlighted)];
    [_kButton rac_liftSelector:@selector(setTitleColor:forState:) withSignals:corlorSignal, eventSignal, nil];

}

- (void)objectSignal {
    if (_testObject == nil)
        _testObject = [TestObject new];

    RACSignal *signal1 = [_testObject rac_signalForSelector:@selector(display)];
    [signal1 subscribeNext:^(id x) {
        NSLog(@"方法被触发");
    }];

    RACSignal *signal2 = [_testObject rac_signalForSelector:@selector(display:)];
    [signal2 subscribeNext:^(id x) {
        NSLog(@"方法带参数被触发: %@", x);
    }];
}

- (void)changeSignal {
    RACSignal *signal1 = [RACSignal return:@"123456789"];
    RACSignal *signal2 = [signal1 map:^id(id value) {
        return [NSString stringWithFormat:@"change:%@", value];
    }];

    [signal2 subscribeNext:^(id x) {
        NSLog(@"...%@", x);
    }];
}

- (void)arraySignal {
    NSArray *array = @[@"123456", @"12345", @"1234", @"123", @"12", @"1"];
    RACSignal *signal1 = array.rac_sequence.signal;
    RACSignal *signal2 = [signal1 filter:^BOOL(NSString *value) {
        if (value.length > 3)
            return YES;
        return NO;
    }];
    [signal2 subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
}

- (void)mergeSignal {
    RACSignal *signal1 = [RACSignal return:@"123"];
    RACSignal *signal2 = [RACSignal return:@"456"];
    RACSignal *signal3 = [signal1 merge:signal2];
    [signal3 subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
}

- (void)KVOSignal {
    [RACObserve(self, self.name) subscribeNext:^(id x) {
        NSLog(@"kvo %@", x);
    }];
}

- (void)racSchedule {

    RACScheduler *mainScheduler = [RACScheduler mainThreadScheduler];
    [mainScheduler schedule:^{
        NSLog(@"%@", [NSThread currentThread]);
    }];

    RACScheduler *scheduler1 = [RACScheduler scheduler];
    [scheduler1 schedule:^{
        NSLog(@"%@", [NSThread currentThread]);
    }];

    RACScheduler *scheduler2 = [RACScheduler scheduler];
    [scheduler2 schedule:^{
        NSLog(@"%@", [NSThread currentThread]);
    }];

    RACSignal *signal2 = [RACSignal return:@"123456"];
    RACSignal *signal1 = [signal2 subscribeOn:scheduler2];
    [signal1 subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


    [scheduler1 afterDelay:2 schedule:^{
        [signal1 subscribeNext:^(id x) {
            NSLog(@"%@", x);
        }];
    }];

    NSLog(@"the end!");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

//    [self dynamicSignal];

//    if (!_hotSignal)
//        [self hotSignal];
//    [_hotSignal sendNext:@"i want to fuck you!"];

//    if(!_testObject)
//        [self objectSignal];
//    [_testObject display:@"fuck you!"];


//    [self changeSignal];

//    [self arraySignal];

//    [self mergeSignal];


//    if(!self.name){
//        self.name = @"123456";
//        [self KVOSignal];
//    }
//
//    self.name = @"fuck you!";

    [self racSchedule];

}


@end
